<html>
<head>
<script type="text/javascript">

function init() {
	if(top.uploadDone) top.uploadDone(); //top means parent frame.
}
window.onload=init;

</script>
</head>
<body>
<?php

$usr_nmbr=$_POST['usr_nmbr'];

list($name,$result) = upload('img','banner/'.$_POST['usr_nmbr'],'jpg,jpeg,gif,png,bmp');
if($name) { // Upload Successful
    
	$usr_nmbr=$_POST['usr_nmbr'];
	$details = stat("banner/".$usr_nmbr);
	//	$details = stat("gallery_data/$albm_nmbr");
	//$size = $details['size'] / 1024;
	print json_encode(array(
		"success"	=>	$result,
		"failure"	=>	false,
		"file_name"	=>	$name,	// Name of the file - JS should get this value
		"size"		=>	null,	// Size of the file - JS should get this as well.
	));
} else { // Upload failed for some reason.
	print json_encode(array(
		"success"	=>	false,
		"failure"	=>	$result,
	));
}

?>
</body>
</html>

<?php
function upload($file_id, $folder, $types="")
{
    $uploaddir = 'banner/'.$_POST['usr_nmbr'];
    
    // here the code should be like this if there is a folder exist then delete its content
    
    	if(!is_dir($uploaddir)){
		if (!mkdir($uploaddir, 0777)) {
			 die('Failed to create folders...');
			 
			 
		}
	}
    $result='';
    $file_names='';
	//$_FILES["img"]["error"] as $index => $status;
       // if ($status == UPLOAD_ERR_OK)
        //{
                if(!$_FILES[$file_id]['name']) return array('','No file specified');
            
                $file_title = $_FILES[$file_id]['name'];
                $file_names=$file_title;
                //Get file extension
                $ext_arr = split("\.",basename($file_title));
                $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension
            

            
                $all_types = explode(",",strtolower($types));
                if($types) {
                    if(in_array($ext,$all_types));
                    else {
                        $result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
                        return array('invalid',$result);
                    }
                }
            
           
                $temp = $_FILES["img"]["tmp_name"];
                $file_ref = $_FILES["img"]["name"];
                $file_location_ori = $uploaddir .'/'. basename($file_ref);
                $file_location = $uploaddir .'/'. 'pp_'.$_POST['usr_nmbr'].'.jpg';
                $file_location = $uploaddir .'/change.jpg';
								
		//

                //$file_location2 = $uploaddir .'/'. 'pp.jpg';		
		if (!move_uploaded_file($temp, $file_location_ori))
                {

                    echo "<p>Oops! Your file (".$file_ref.") was not uploaded.</p>";
		    //return array("invalid file","r");
                }
		else
		{
			//copy($file_location, $file_location_ori);
			crop($file_location_ori, $uploaddir.'/banner', 940, 350);
			crop($file_location_ori, $uploaddir."/small", 50, 50);
			crop($file_location_ori, $uploaddir.'/pp_'.$_POST['usr_nmbr'], 150, 150);
			crop($file_location_ori, $uploaddir.'/change', 150, 150);
		}


	return array($file_location,$result);
}


function crop($filename, $dst, $new_width, $new_height){
	//get width and height
	list($width, $height) = getimagesize($filename);
	//get mime type
	$imgInfo = getimagesize($filename);
	$mime_type=$imgInfo['mime'];
	
	//include("php/inc/BMP.php");
	
	//destination image
	$image_p = imagecreatetruecolor($new_width, $new_height);
	
	//set proper mime type for destination
	if($mime_type=="image/jpeg")
		$image = imagecreatefromjpeg($filename);
	elseif($mime_type=="image/png")
		$image = imagecreatefrompng($filename);
	elseif($mime_type=="image/gif")
		$image = imagecreatefromgif($filename);
	elseif($mime_type=="image/bmp")
	{
		
		$image = imagecreatefrombmp($filename);
		//$image = imagecreatefrombmp($filename);
	}
		
	//resample
	
	//if 1:1
	if($width==$height){
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	}
	
	//if width> height
	elseif($width>$height){
		imagecopyresampled($image_p, $image,  0, 0, (($width-$height)/2), 0, $new_width, $new_height, $width-($width-$height), $height);
	}
	// if height>width
	elseif($width<$height){
		imagecopyresampled($image_p, $image, 0, 0, 0, (($height-$width)/2), $new_width, $new_height, $width, $height-($height-$width));
	}
	/*
	// if height>width
	elseif($width>$height){
		imagecopyresampled($image_p, $image, 0, 0, (($width-$new_width)/2), (($height-$new_height)/2), 940, 350, $width, $height);
	}
		elseif($width>$height and $width > $new_width and $height>$new_height){
		imagecopyresampled($image_p, $image, 0, 0, (($width-$new_width)/4), (($height-$new_height)/4), 940, 350, (($width-$new_width)/2 + $new_width), (($height-$new_height)/2 + $new_height));
	}	
	*/
	// Output
		imagejpeg($image_p, $dst.".jpg", 100);
	/*
	if($mime_type=="image/jpeg")
		imagejpeg($image_p, $dst.".jpg", 100);
	elseif($mime_type=="image/png")
		imagepng($image_p, $dst.".png", 100);
	elseif($mime_type=="image/bmp")
		imagebmp($image_p, $dst.".bmp", 100);
	elseif($mime_type=="image/gif")
		imagegif($image_p, $dst.".gif", 100);
	elseif($mime_type=="image/psd")
		imagepsd($image_p, $dst.".psd", 100);
	*/
}

function imagecreatefrombmp($p_sFile) {
    $file = fopen($p_sFile, "rb");
    $read = fread($file, 10);
    while (!feof($file) && ($read <> ""))
        $read .= fread($file, 1024);
    $temp = unpack("H*", $read);
    $hex = $temp[1];
    $header = substr($hex, 0, 108);
    if (substr($header, 0, 4) == "424d") {
        $header_parts = str_split($header, 2);
        $width = hexdec($header_parts[19] . $header_parts[18]);
        $height = hexdec($header_parts[23] . $header_parts[22]);
        unset($header_parts);
    }
    $x = 0;
    $y = 1;
    $image = imagecreatetruecolor($width, $height);
    $body = substr($hex, 108);
    $body_size = (strlen($body) / 2);
    $header_size = ($width * $height);
    $usePadding = ($body_size > ($header_size * 3) + 4);
    for ($i = 0; $i < $body_size; $i+=3) {
        if ($x >= $width) {
            if ($usePadding)
                $i += $width % 4;
            $x = 0;
            $y++;
            if ($y > $height)
                break;
        }
        $i_pos = $i * 2;
        $r = hexdec($body[$i_pos + 4] . $body[$i_pos + 5]);
        $g = hexdec($body[$i_pos + 2] . $body[$i_pos + 3]);
        $b = hexdec($body[$i_pos] . $body[$i_pos + 1]);
        $color = imagecolorallocate($image, $r, $g, $b);
        imagesetpixel($image, $x, $height - $y, $color);
        $x++;
    }
    unset($body);
    return $image;
}

 

?>